import { Body, Controller, Post, Res, Header, HttpException } from "@nestjs/common";
import { Response } from "express";
import { AuthService } from "./auth.service";
import { AuthSignInDto, AuthSignUpDto } from "src/dto/auth";
import { ApiRes, Public } from "src/decorator";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { UserEntity } from "src/entity";
import { UserInfo } from "src/common/model";

@ApiTags("Auth - 认证模块")
@Public()
@Controller("auth")
export class AuthController {
    constructor(private readonly authService: AuthService) {}

    @ApiOperation({ summary: "登录" })
    @ApiRes({ type: UserInfo })
    @Header("Access-Control-Expose-Headers", "Authorization")
    @Post("signin")
    async signin(@Body() user: AuthSignInDto, @Res({ passthrough: true }) response: Response) {
        const [token, info] = await this.authService.signin(user);
        response.setHeader("Authorization", "Bearer " + token);
        return info;
    }

    @ApiOperation({ summary: "注册" })
    @ApiRes({ type: UserInfo })
    @Header("Access-Control-Expose-Headers", "Authorization")
    @Post("signup")
    async signup(@Body() user: AuthSignUpDto, @Res({ passthrough: true }) response: Response) {
        const [token, info] = await this.authService.signup(user);
        response.setHeader("Authorization", "Bearer " + token);
        return info;
    }
}
